<?php

use yii\helpers\Inflector;
use yii\helpers\StringHelper;

/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */

$urlParams = $generator->generateUrlParams();
$nameAttribute = $generator->getNameAttribute();
$searchModelName = preg_replace('/(.*)\\\\/', '', $generator->searchModelClass);
$moduleName = preg_replace(['/common\\\modules\\\/', '/\\\(.*)/'], '', $generator->modelClass);
$jsonFields = [];
$fieldMap = [];
?>
<template>
    <d2-container>
        <search-form :fields-set="searchFields"
                     :values="loadListParams[searchModelName]"
                     #header
                     :is-loading="isLoading"
                     @submit="searchLoad"
                     @change="changeSearchForm"
                     :maps="fieldMap"></search-form>
        <el-row>
            <el-button type="primary" @click="add">增加</el-button>
        </el-row>
        <el-table class="m-t-md" :data="list" v-loading="isLoading" border stripe @sort-change="changeSort">
            <?php
            $count = 0;

            $tableSchema = $generator->getTableSchema();
            if ($tableSchema === false) {
                foreach ($generator->getColumnNames() as $name) {
                    $attr = '';
                    ?>

                    <el-table-column prop="<?=$name?>" label="<?=preg_replace('/(\(|（|：|:)(.*)/U', '', $name)?>" class-name="ellipsis"></el-table-column>
            <?php
                }
            } else {
                foreach ($tableSchema->columns as $column) {
                    $format = $generator->generateColumnFormat($column);
                    if ($column->type === 'json') continue;
                    ?>

                    <el-table-column prop="<?=$column->name?>" label="<?=preg_replace('/(\(|（|：|:)(.*)/', '', $column->comment)?>"<?=$column->type == 'datetime' ? ' width="100px" class-name="ellipsis"' : ''?>><?php
                        switch ($column->type) {
                            case 'tinyint':
                                if (preg_match('/[:：]/', $column->comment)) {
                                    $set = preg_replace('/(.*)(:|：)/is', '', $column->comment);
                                    $set = preg_replace('/\s+/', ' ', $set);
                                    $arr = explode(' ', $set);
                                    foreach ($arr as $v) {
                                        $key = intval($v);
                                        if (is_numeric($key)) {
                                            if ($key == 0 && count($arr) <= 1) break;
                                            $fieldMap[$column->name][$key] = str_replace($key, '', $v);
                                        }
                                    }
                                }
                                ?>

                        <template v-slot:default="scope">
                            {{fieldMap.<?=$column->name?>[scope.row.<?=$column->name?>]}}
                        </template>
                    <?php
                                break;
                            case 'boolean':
                                ?>

                        <template v-slot:default="scope">
                            {{scope.row.<?=$column->name?> === '1' ? '是' : '否'}}
                        </template>
                        <?php
                            break;
                        }
                    ?></el-table-column>
                    <?php
                }
            }
            ?>
            <el-table-column label="操作" width="200px" fixed="right">
                <template v-slot:default="scope">
                    <el-button size="mini" @click="updateInfo(scope.$index, scope.row)">修改</el-button>
                    <el-popconfirm title="确定删除吗？" @confirm="deleteInfo(scope.$index, scope.row.<?=$tableSchema->primaryKey[0]?>)">
                        <el-button size="mini" type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-row type="flex" class="row-bg" justify="center" slot="footer">
            <xing-pagination :pagination="pagination"
                             v-model="pagination.page"
                             @changeSize="changePageSize"
                             @changePage="changeCurrentPage"></xing-pagination>
        </el-row>
        <!--    修改资料表单-->
        <xing-dialog
                title="编辑信息"
                v-model="showForm"
                v-if="showForm"
                :is-load="isLoading"
                :close-on-click-modal="false">
            <div class="dialog-content">
                <form-group :fields-set="fieldsSet"
                            :values="postData"
                            :maps="fieldMap"
                            module="<?= str_replace('Search', '', $searchModelName)?>"
                            @change="changeForm"></form-group>
            </div>
            <template #footer>
                <el-button type="primary" @click="submitForm">保存</el-button>
                <el-button @click="toggle('showForm')">关闭</el-button>
            </template>
        </xing-dialog>
    </d2-container>
</template>

<script>
    import AdminListMixin from '@/mixins/request/AdminListMixin'
    import SearchForm from '@/components/search/search-form'
    import FormListMixin from '@/mixins/form/FormListMixin'
    import FormGroup from '@/components/form/form-group'
<?php

?>
    export default {
        mixins: [AdminListMixin, FormListMixin],
        components: {
            FormGroup,
            SearchForm
        },
        data () {
            return {
                fieldMap: <?=json_encode($fieldMap, 384)?>,
                primaryKey: '<?=$tableSchema->primaryKey[0]?>',
                modelUrl: '<?=preg_replace(['/\\\views\\\/', '/(.*)\\\/'], ['/', ''], $generator->viewPath) ?: $moduleName . '/' . $generator->getControllerID()?>',
                searchModelName: '<?= $searchModelName ?>',
                fieldsSet: [
                    <?php
                    $count = 0;
                    $tableSchema = $generator->getTableSchema();
                    if ($tableSchema === false) {
                    foreach ($generator->getColumnNames() as $name) {
                    ?>
                    { name: '<?=$name?>>', key: '<?=$name?>', type: 'input' }
                    <?php
                    }
                    } else {
                    foreach ($tableSchema->columns as $column) {
                    $type = '';
                    switch ($column->type) {
                        case 'tinyint':
                            $type = 'picker';
                            break;
                        case 'json':
                            $type = 'select';
                            $jsonFields[] = $column->name;
                            break;
                        case 'string':
                            $type = $column->size >= 500 ? 'textarea' : 'input';
                            break;
                        case 'datetime':
                            $type = 'datetime';
                            break;
                        case 'date':
                            $type = 'date';
                            break;
                        case 'boolean':
                            $type = 'boolean';
                            break;
                        default:
                            $type = 'input';
                            break;
                    }
                    ?>
    { name: '<?=preg_replace('/(\(|（|：|:)(.*)/', '', $column->comment)?>', key: '<?=$column->name?>', type: '<?=$type?>', placeholder: '' },
                    <?php
                    }
                    }
                    ?>
],
                searchFields: [

                ],
              <?php if (!empty($jsonFields)) {?>
  fieldJson: ['<?= implode("', '", $jsonFields) ?>']
  <?php } ?>
            }
        },
        created (o) {

        },
        methods: {}
    }
</script>

<style scoped lang="scss">

</style>
